package cuongnh.project.japanesediary.vocabulary.ui.wizard.page;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.beans.BeanProperties;
import org.eclipse.core.databinding.beans.BeansObservables;
import org.eclipse.core.databinding.observable.list.IObservableList;
import org.eclipse.core.databinding.observable.map.IObservableMap;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.core.databinding.property.Properties;
import org.eclipse.jface.databinding.swt.WidgetProperties;
import org.eclipse.jface.databinding.viewers.ObservableListContentProvider;
import org.eclipse.jface.databinding.viewers.ObservableMapLabelProvider;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerFilter;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import cuongnh.project.japanesediary.vocabulary.common.CommonServices;
import cuongnh.project.japanesediary.vocabulary.model.Category;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Control;

public class NewCategoryWizardPage extends WizardPage {
	private DataBindingContext m_bindingContext;
	private ComboViewer cbParent;
	private Text txtName;
	private Text txtDescription;
	private Category category;
	private CommonServices commonServices;
	private List<Category> parentList;

	/**
	 * Create the wizard.
	 */
	public NewCategoryWizardPage(Category category,
			CommonServices commonServices) {
		super("wizardPage");
		setTitle("New Category");
		setDescription("Wizard to create a new Category");
		this.category = category;
		this.commonServices = commonServices;
		initParentList();
	}

	private void initParentList() {
		if(category.getParentCategory() != null){
			parentList = new ArrayList<Category>();
			parentList.add(category.getParentCategory());
		}else{
			parentList = commonServices.getCategoryDao().getAll();
		}		
	}

	/**
	 * Create contents of the wizard.
	 * 
	 * @param parent
	 */
	public void createControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NULL);
		container.setLayout(new GridLayout(2, false));

		Label lblParentCategory = new Label(container, SWT.NONE);
		lblParentCategory.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER,
				false, false, 1, 1));
		lblParentCategory.setText("Parent category");

		cbParent = new ComboViewer(container, SWT.BORDER);
		Combo combo = cbParent.getCombo();
		cbParent.addFilter(new ViewerFilter() {

			@Override
			public boolean select(Viewer viewer, Object parentElement,
					Object element) {
				Category category = (Category) element;
				if (category.getCategoryName().toLowerCase()
						.contains(cbParent.getCombo().getText().toLowerCase())) {
					return true;
				}
				
				return false;
			}			
		});
		
		cbParent.getCombo().setLayoutData(
				new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		Label lblName = new Label(container, SWT.NONE);
		lblName.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false,
				1, 1));
		lblName.setText("Name");

		txtName = new Text(container, SWT.BORDER);
		txtName.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1,
				1));

		Label lblDescription = new Label(container, SWT.NONE);
		lblDescription.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		lblDescription.setText("Description");

		txtDescription = new Text(container, SWT.BORDER | SWT.MULTI);
		GridData gd_txtDescription = new GridData(SWT.FILL, SWT.FILL, true,
				true, 1, 1);
		gd_txtDescription.heightHint = 153;
		txtDescription.setLayoutData(gd_txtDescription);
		setControl(container);
		container.setTabList(new Control[]{txtName, txtDescription, combo});
		m_bindingContext = initDataBindings();

		// Set default parent category
		if (category.getParentCategory() != null) {
			cbParent.setSelection(new StructuredSelection(category
					.getParentCategory()));
		}

	}

	public Category getCategory() {
		return category;
	}

	protected DataBindingContext initDataBindings() {
		DataBindingContext bindingContext = new DataBindingContext();
		//
		IObservableValue observeTextTxtNameObserveWidget = WidgetProperties
				.text(SWT.Modify).observe(txtName);
		IObservableValue categoryNameNewOneObserveValue = BeanProperties.value(
				"categoryName").observe(category);
		bindingContext.bindValue(observeTextTxtNameObserveWidget,
				categoryNameNewOneObserveValue, null, null);
		//
		IObservableValue observeTextTxtDescriptionObserveWidget = WidgetProperties
				.text(SWT.Modify).observe(txtDescription);
		IObservableValue descriptionNewOneObserveValue = BeanProperties.value(
				"description").observe(category);
		bindingContext.bindValue(observeTextTxtDescriptionObserveWidget,
				descriptionNewOneObserveValue, null, null);
		//
		ObservableListContentProvider listContentProvider = new ObservableListContentProvider();
		IObservableMap observeMap = BeansObservables.observeMap(
				listContentProvider.getKnownElements(), Category.class,
				"categoryName");
		cbParent.setLabelProvider(new ObservableMapLabelProvider(observeMap));
		cbParent.setContentProvider(listContentProvider);
		//
		IObservableList selfList = Properties.selfList(Category.class).observe(
				parentList);
		cbParent.setInput(selfList);
		//

		return bindingContext;
	}
	
	public Category getParentCategory(){
		if(cbParent.getSelection() != null){
			return (Category)((StructuredSelection)cbParent.getSelection()).getFirstElement();
		}
		
		return null;
	}

}
